﻿using System;
using System.ComponentModel;
using System.Collections.Specialized;
using System.Reflection;
using System.Runtime.Serialization;

namespace Sistema.Dominio
{
    [DataContract]
    public abstract class Entidade : INotifyPropertyChanged, IDisposable, IEditableObject
    {
        private Int32 _Id;
        private Int32 _IdEmpresa;
        private DateTime _DataOperacao;
        private String _UsuarioOperacao;
        private String _ComputadorOperacao;

        public Entidade()
        {
            _DataOperacao = DateTime.Now;
            _UsuarioOperacao = string.Empty;
            _ComputadorOperacao = string.Empty;
        }

        #region get's and Set's

        [DataMember]
        public virtual Int32 ID
        {
            get { return _Id; }
            set { this._Id = value; }
        }
        [DataMember]
        public virtual Int32 IdEmpresa
        {
            get { return _IdEmpresa; }
            set { _IdEmpresa = value; }
        }
        [DataMember]
        public virtual DateTime DataOperacao
        {
            get { return _DataOperacao; }
            set { _DataOperacao = value; }
        }
        [DataMember]
        public virtual String UsuarioOperacao
        {
            get { return _UsuarioOperacao; }
            set { _UsuarioOperacao = value; }
        }
        [DataMember]
        public virtual String ComputadorOperacao
        {
            get { return _ComputadorOperacao; }
            set { _ComputadorOperacao = value; }
        }
        #endregion

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void RaisePropertyChanged(string propertyname)
        {
            try
            {
                PropertyChangedEventHandler handler = this.PropertyChanged;
                if (handler != null)
                {
                    handler(this, new PropertyChangedEventArgs(propertyname));
                }
            }
            catch
            {
                return;
            }
        }
        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            this.PropertyChanged = null;
        }

        #endregion

        #region IEditableObject Members

        private HybridDictionary oldState;

        public void BeginEdit()
        {
            this.oldState = new HybridDictionary();
            foreach (PropertyInfo property in this.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance))
            {
                if (property.CanWrite)
                {
                    oldState[property.Name] = property.GetValue(this, null);
                }
            }
        }

        public void CancelEdit()
        {
            if (this.oldState == null) { return; }
            foreach (PropertyInfo property in this.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance))
            {
                if (property.CanWrite)
                {
                    property.SetValue(this, oldState[property.Name], null);
                }
            }
        }

        public void EndEdit()
        {
            this.oldState = null;
        }

        #endregion
        }
}